<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <button v-for="item in btnsList" :key="item.name" @click='handleClick(item.name,item.title)'>{{item.title}}</button>
        <keep-alive exclude='sports'>
            <component :is='comp'></component>
        </keep-alive>
    </div>
    <template id="news">
        <div>
            <p>新闻</p>
            <input type="text" v-model='val1' />
        </div>
    </template>
    <template id="happy">
        <div>
            <p>娱乐</p>
            <input type="text" v-model="val2" />
        </div>
    </template>
    <template id="sports">
        <div>
            <p>体育</p>
            <input type="text" v-model="val3" />
        </div>
    </template>
    <script src="../vue.js"></script>
    <script>
        let news={
            template:'#news',
            data () {
                return {
                    val1:'',
                }
            },
            activated () {
                console.log('new-activated');
            },
            deactivated () {
                console.log('new-deactivated');
            }
        }
        let happy={
            template:'#happy',
            data () {
                return {
                    val2:'',
                }
            }
        }
        let sports={
            template:'#sports',
            data () {
                return {
                    val3:'',
                }
            }
        }
        new Vue({
            el:'#app',
            data () {
                return {
                    btnsList:[
                        {name:'news',title:'新闻'},
                        {name:'happy',title:'娱乐'},
                        {name:'sports',title:'体育'},
                    ],
                    comp:'news'
                }
            },
            components: {
                news,
                happy,
                sports,
            },
            methods: {
                handleClick(name,title){
                    // console.log(name);
                    // console.log(title);
                    if(name=='news')
                        this.comp='news';
                    else if(name=='happy')
                        this.comp=name;
                    else if(name=='sports')
                        this.comp=name;
                }
            }
        })
    </script>
</body>
</html>